local jilun = fk.CreateSkill{
  name = "wk_heg__jilun",
}

Fk:loadTranslationTable{
  ["wk_heg__jilun"] = "机论",
  [":wk_heg__jilun"] = "①准备阶段，你修改一项你的“合纵”规则至你下回合开始；②其他角色弃置你的牌后，你可以检索并获得一张基本牌或带有“合纵”标记的牌。<br />"..
  "<font color = 'orange'>合纵修改方式：1.删去“出牌阶段限一次”限制；2.删去“至多三张”限制；3.删去“与你势力不同或未确定势力的角色”限制</font>",
  
  ["wk_heg__jilun_change1"] = "合纵A 取消“出牌阶段限一次”限制",
  ["wk_heg__jilun_change2"] = "合纵B 取消“至多三张手牌”限制",
  ["wk_heg__jilun_change3"] = "合纵C 取消“其他势力角色”限制",

  ["$wk_heg__jilun1"] = "时移不移，违天之祥也。",
  ["$wk_heg__jilun2"] = "民望不因，违人之咎也。",
}

local HG = require "packages.new_heg_diy_my.util"

jilun:addEffect(fk.AfterCardsMove,{
  can_trigger = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.moveReason == fk.ReasonDiscard and move.from and move.from == player and not player.dead and move.proposer and move.proposer ~= player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            return player:hasSkill(jilun.name)
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    -- room:askToDiscard(room.current, {
    --   min_num = 1,
    --   max_num = 1,
    --   include_equip = true,
    --   skill_name = jilun.name,
    --   cancelable = false,
    -- })
    local card
    local cards = {}
    local _pattern = ".|.|.|.|.|basic"
    if #room:getCardsFromPileByRule(_pattern, 1) == 0 then return false end
    while true do
      local id = room:getNCards(1)[1]
      room:moveCardTo(id, Card.Processing, nil, fk.ReasonJustMove, jilun.name)
      room:delay(300)
      local c = Fk:getCardById(id)
      if c:getTypeString() == "basic" or HG.HasMark(c, "@@alliance-public", MarkEnum.CardTempMarkSuffix) then
        card = c
        break
      else
        table.insert(cards, id)
      end
    end
    room:moveCardTo(card.id, Player.Hand, player, fk.ReasonPrey, jilun.name, nil, true, player.id)
    cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
    room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, jilun.name, nil, true, player.id)
  end,
})

jilun:addEffect(fk.EventPhaseStart,{
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(jilun.name) and player == target and player.phase == Player.Start
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local choices = {"wk_heg__jilun_change1", "wk_heg__jilun_change2", "wk_heg__jilun_change3"}
      local choice = room:askToChoice(player, { choices = choices, skill_name = jilun.name})
      local mark_num = 0
      if choice == "wk_heg__jilun_change1" then
        mark_num = 1
        room:handleAddLoseSkills(player, "alliance1&", nil)
      elseif choice == "wk_heg__jilun_change2" then
        mark_num = 2
        room:handleAddLoseSkills(player, "alliance2&", nil)
      else
        mark_num = 3
        room:handleAddLoseSkills(player, "alliance3&", nil)
      end
      room:setPlayerMark(player, "wk_heg__jilun_alliance", mark_num)
    end
  end,
})


jilun:addEffect(fk.TurnStart, {
  can_refresh = function(self, event, target, player, data)
    return player:getMark("wk_heg__jilun_alliance") > 0 and player == target
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark_num = player:getMark("wk_heg__jilun_alliance")
    if mark_num == 1 then
      room:handleAddLoseSkills(player, "-alliance1&", nil)
    elseif mark_num == 2 then
      room:handleAddLoseSkills(player, "-alliance2&", nil)
    elseif mark_num == 3 then
      room:handleAddLoseSkills(player, "-alliance3&", nil)
    end
    room:setPlayerMark(player, "wk_heg__jilun_alliance", 0)
  end,
})

jilun:addEffect(fk.TurnEnd, {
  can_refresh = function(self, event, target, player, data)
    return player:getMark("wk_heg__jilun_alliance_change") > 0 and player == target
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark_num = player:getMark("wk_heg__jilun_alliance_change")
    if mark_num == 1 then
    room:handleAddLoseSkills(player, "-alliance1&", nil)
    elseif mark_num == 2 then
    room:handleAddLoseSkills(player, "-alliance2&", nil)
    elseif mark_num == 3 then
    room:handleAddLoseSkills(player, "-alliance3&", nil)
    end
    room:setPlayerMark(player, "wk_heg__jilun_alliance_change", 0)
  end,
})

jilun:addEffect(fk.EventPhaseStart, {
  can_refresh = function(self, event, target, player, data)
    if not (player == target and ((player:getMark("wk_heg__jilun_alliance") > 0) or (player:getMark("wk_heg__jilun_alliance_change") > 0))) then return false end
    return player.phase == Player.Play
  end,
  on_refresh = function(self, event, target, player, data)
    player:setSkillUseHistory("alliance&", 99, Player.HistoryPhase)
  end,
})

return jilun
